﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Sparrow.CMS
{
    using Sparrow.Extensions;
    using Sparrow.CMS.Providers;

    public static class Facotry
    {
        public static IPageContentProvider Provider
        {
            get
            {
                return CallContext.Current.Resolve<IPageContentProvider>(() => CallContext.Current.Resolve<IPageContentProvider>("default"));
            }
        }

        public static IViewEngine FindViewEngine(string templateName)
        {

            FileInfo fi = new FileInfo(templateName);

            return CallContext.Components.Find<IViewEngine>(fi.Extension.ToLower());
            
        }

        public static IEnumerable<IAction> Actions
        {
            get
            {
                return CallContext.Components.FindAll<IAction>();
            }
        }

        public static IEnumerable<Layout> Layouts
        {
            get
            {
                if (Directory.Exists(LayoutFolder))
                {
                    var folder = new DirectoryInfo(LayoutFolder);
                    var layouts = folder.GetFiles();

                    foreach (var layout in layouts)
                    {
                        yield return new Layout { Name = layout.Name };
                    }
                }
                else
                {
                    yield return default(Layout);
                }
            }
        }

        static string _LayoutFolder;
        static string LayoutFolder
        {
            get
            {
               _LayoutFolder =  _LayoutFolder.LazyInit(()=> Environments.ApplicationPath +"\\Layouts");

               return _LayoutFolder;
            }
        }
    }
}
